package simpleGeom;

import java.awt.geom.Point2D;

import processing.core.PVector;

public class Intersector2D implements Comparable {

	public HasPos2D segmentSegmentIntersection(HasPos2D A, HasPos2D B,
			HasPos2D C, HasPos2D D) {
		return segmentSegmentIntersection(A.getX(), A.getY(), B.getX(),
				B.getY(), C.getX(), C.getY(), D.getX(), D.getY());
	}

	float s;
	float r;
	boolean parallel;

	public Vec2D linelineIntersection(float aX, float aY, float bX, float bY,
			float cX, float cY, float dX, float dY) {
		float dX1 = bX - aX;
		float dY1 = bY - aY;
		float denominator = ((dX1) * (dY - cY)) - (dY1 * (dX - cX));

		if (denominator == 0) {
			parallel = true;
			return null;
		}// parallel

		float numerator = ((aY - cY) * (dX - cX)) - (aX - cX) * (dY - cY);
		float numerator2 = ((aY - cY) * (bX - aX)) - ((aX - cX) * (bY - aY));

		r = numerator / denominator;
		s = numerator2 / denominator;

		float x = aX + r * (dX);
		float y = aY + r * (dY);
		return new Vec2D(x, y);
	}

	public HasPos2D segmentSegmentIntersection(float aX, float aY, float bX,
			float bY, float cX, float cY, float dX, float dY) {
		Vec2D v = linelineIntersection(aX, aY, bX, bY, cX, cY, dX, dY);
		if (v == null)return null;
		if ((r >= 0 && r <= 1) && (s >= 0 && s <= 1))return v;
		return null;
	}
	public HasPos2D segmentLineIntersection(float aX, float aY, float bX,
			float bY, float cX, float cY, float dX, float dY) {
		Vec2D v = linelineIntersection(aX, aY, bX, bY, cX, cY, dX, dY);
		if (v == null)return null;
		if (r >= 0 && r <= 1) return v;
		return null;
	}
	HasLine2D line1;
	HasLine2D line2;
	float t1;
	float t2;
	float x;
	float y;
	public HasLine2D getLine1() {
		return line1;
	}
	public void setLine1(HasLine2D line1) {
		this.line1 = line1;
	}
	public HasLine2D getLine2() {
		return line2;
	}
	public void setLine2(HasLine2D line2) {
		this.line2 = line2;
	}
	public float getT1() {
		return t1;
	}
	public void setT1(float t1) {
		this.t1 = t1;
	}
	public float getT2() {
		return t2;
	}
	public void setT2(float t2) {
		this.t2 = t2;
	}


	public int compareTo(Object o) {
		return 0;
	}
	public float getX() {
		return x;
	}
	public void setX(float x) {
		this.x = x;
	}
	public float getY() {
		return y;
	}
	public void setY(float y) {
		this.y = y;
	}
}
